package de.geckocraft.iConomyExchange.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;




import org.bukkit.plugin.Plugin;

import java.sql.DatabaseMetaData;

public class broker
{
	static Connection con = null;
	static Statement stmt = null;
	static ResultSet rs = null;
	static DatabaseMetaData md = null;
	
	public static void connect()
	{		
		String address = "jdbc:mysql://"
		+ mysqlconfig.getconfig().getString("ip") 
		+ ":" + mysqlconfig.getconfig().getString("port") 
		+ "/" + mysqlconfig.getconfig().getString("database"); 

		try
		{
			con = DriverManager.getConnection(address, mysqlconfig.getconfig().getString("user"), mysqlconfig.getconfig().getString("password"));
			stmt = con.createStatement();
			md = con.getMetaData();
			
		}
		catch(SQLException e)
		{
			Plugin p = de.geckocraft.iConomyExchange.main.main.getPlugin();
			p.getPluginLoader().disablePlugin(p);
			System.out.println("[" + p.getName() + "]Can't connect to Database");
			System.out.println(e);
		}

	}
	
	public static ResultSet sendStatement(String statement)
	{
		rs = null;
		try
		{
			//System.out.println(statement);
			rs = stmt.executeQuery(statement);
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
				
		return rs;
	}
	
	public static void sendUpdate(String statement)
	{
		try
		{
			//System.out.println(statement);
			stmt.executeUpdate(statement);
		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
	}
	
	public static boolean tableexist(String tablename)
	{
		boolean exist = false;
		
		try
		{
			String[] types = {"TABLE"};
			rs = md.getTables(null, null, tablename, types);
			while (rs.next()) 
			{
			exist = true;
			}
			
		} catch (SQLException e)
		{
			e.printStackTrace();
		} 
		
		return exist;
		
	}
	

}
